/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.hamacekj.dotaznikserver;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Otázka s textovou odpovědí.
 * @author Jan Hamáček
 */
public class TextQuestion implements Question {

    public TextQuestion() {
    }
    
    public TextQuestion(int id, String message) {
        this.message = message;
        this.id = id;
    }
    

    @Override
    public void send(Connection c) throws IOException {
        c.writeLine("ASK TEXT "+this.id);
        String[] splitted = this.message.split(System.lineSeparator());
        for(String line: splitted){
            c.writeLine("MSG "+line);
        }
        c.writeLine(".");
    }

    @Override
    public void addReply(Connection source, String[] reply) {
        try{
            try{
                String[] linearr = reply[0].split(" ");
                if(!"REPLY".equals(linearr[0])){
                    throw new ProtocolException("REPLY type expected");
                }
                if(!"TEXT".equals(linearr[1])){
                    throw new ProtocolException("YN type expected");
                }
                StringBuilder replyTextBuilder = new StringBuilder();
                for(int i = 1; i < reply.length; i++){
                    String[] splittedLine = reply[i].split(" ", 2);
                    try{
                        if("MSG".equals(splittedLine[0])){
                            replyTextBuilder.append(splittedLine[1]).append(" ");
                        }
                    }catch(IndexOutOfBoundsException e){}
                }
                String replyText = replyTextBuilder.toString();
                replies.put(source, replyText);
                addTableRow(source.toString(), replyText);
            }catch(IndexOutOfBoundsException e){
                throw new ProtocolException("Incomplete reply line. Index out of bounds", e);
            }
        }catch(ProtocolException e){
            // TODO: log protocol exception
        }
    }

    @Override
    public String getShortDesc() {
        if(this.message != null && !"".equals(this.message)){
            return "TEXT: "+this.message;
        }else{
            return "TEXT: "+ date.get(Calendar.HOUR_OF_DAY)+":"+date.get(Calendar.MINUTE);
        }
    }

    @Override
    public String toString() {
        return getShortDesc();
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    @Override
    public int getId() {
        return this.id;
    }

    @Override
    public Container getContainer() {
        if(container == null){
            setupContainer();
        }
        return container;
    }
    
    private void setupContainer(){
        container = new JPanel();
        internalPanel = new JPanel();
        internalPanel.setLayout(new GridBagLayout());
        internalPanel.setBackground(Color.WHITE);
        container.setBackground(Color.WHITE);
        container.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1; c.weighty = 1; c.anchor = GridBagConstraints.NORTHWEST;
        container.add(internalPanel, c);
        numberRows = 0;
    }
    
    private void addTableRow(String label, String text){
        GridBagConstraints c = new GridBagConstraints();
        c.gridy = ++numberRows; c.anchor = GridBagConstraints.NORTH;
        c.fill = GridBagConstraints.BOTH;
        JLabel jLabel = new JLabel(" "+label+" ");
        jLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        
        internalPanel.add(jLabel, c);
        c.gridx = 1; c.weightx = 1;
        jLabel = new JLabel("<html>&nbsp;"
                +text.replaceAll(System.lineSeparator(), "<br>")+"</html>");
        jLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        internalPanel.add(jLabel, c);
        container.validate();
        container.repaint();
    }
    
    private String message;
    private int id = -1;
    private Map<Connection, String> replies = new HashMap<>();
    private Calendar date = Calendar.getInstance();
    
    private JPanel container = null;
    private JPanel internalPanel = null;
    private int numberRows;
}
